using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization;
using System.Drawing.Design;

namespace Skywave.Windows.Forms
{
    public partial class SheetBox : UserControl, IPrintOn
    {
        public SheetBox()
        {
            InitializeComponent();
            mSheet = new Sheet();
            mSheet.PrintAreaRow = 3;
            mSheet.PrintAreaColumn = 5;
            mSheet.Range(1, 1, 3, 5).SetBorder(SheetRangeBorderTypes.Outer, 2);
            mSheet.Range(1, 1, 3, 5).SetBorder(SheetRangeBorderTypes.Inner, 1);
            RefreshImage();
        }

        private Sheet mSheet;
        [Editor(typeof(DataSheetPropertyEditor), typeof(UITypeEditor))]
        public Sheet DataSheet
        {
            get { return mSheet; }
            set { mSheet = value; RefreshImage(); }
        }

        private Image mImage;
        private void RefreshImage()
        {
            Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            Graphics g = Graphics.FromImage(bmp);
            mSheet.PrintOn(g, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height), true, true, false);
            mImage = bmp as Image;
            pictureBox1.Image = mImage;
        }

        //protected override void OnPaint(PaintEventArgs e)
        //{
        //    base.OnPaint(e);
        //    mSheet.PrintOn(e.Graphics, new Rectangle(0, 0, Width, Height), true, true, false);
        //}

        private void SheetBox_Paint(object sender, PaintEventArgs e)
        {
            //mSheet.PrintOn(e.Graphics, new Rectangle(0, 0, Width, Height), true, true, false);
        }

        //protected override void OnPaintBackground(PaintEventArgs e)
        //{
        //    base.OnPaintBackground(e);
        //}
        //protected override void NotifyInvalidate(Rectangle invalidatedArea)
        //{
        //    base.NotifyInvalidate(this.DisplayRectangle);
        //}
        //private void SheetBox_Paint(object sender, PaintEventArgs e)
        //{
        //    //mSheet.PrintOn(e.Graphics, e.ClipRectangle, true, true, false);
        //}


        //public SheetRange SelectedSheetRange
        //{
        //    get { return GetSelSheetRange(); }
        //}

        //private int mSelectedSheetRange_Row1 = 1;
        //[Category("SelectedSheetRange")]
        //public int Row1
        //{
        //    get { return mSelectedSheetRange_Row1; }
        //    set { mSelectedSheetRange_Row1 = value; }
        //}
        //private int mSelectedSheetRange_Row2 = 1;
        //[Category("SelectedSheetRange")]
        //public int Row2
        //{
        //    get { return mSelectedSheetRange_Row2; }
        //    set { mSelectedSheetRange_Row2 = value; }
        //}
        //private int mSelectedSheetRange_Col1 = 1;
        //[Category("SelectedSheetRange")]
        //public int Col1
        //{
        //    get { return mSelectedSheetRange_Col1; }
        //    set { mSelectedSheetRange_Col1 = value; }
        //}
        //private int mSelectedSheetRange_Col2 = 1;
        //[Category("SelectedSheetRange")]
        //public int Col2
        //{
        //    get { return mSelectedSheetRange_Col2; }
        //    set { mSelectedSheetRange_Col2 = value; }
        //}

        //private SheetRange GetSelSheetRange()
        //{
        //    return mSheet.Range(mSelectedSheetRange_Row1, mSelectedSheetRange_Col1, mSelectedSheetRange_Row2, mSelectedSheetRange_Col2);
        //}

        //[Category("SelectedSheetRange")]
        //public Font _Font
        //{
        //    get
        //    {
        //        //Object x = GetSelSheetRange().Font;
        //        //return (x == null) ? new Font("Tahoma", 10) : (Font)x;
        //        return mSheet.Defaults.Font;
        //    }
        //    set { GetSelSheetRange().Font = value; this.Invalidate(); }
        //}

        //[Category("SelectedSheetRange")]
        //public Color _BackColor
        //{
        //    get { return GetSelSheetRange().BackColor; }
        //    set { GetSelSheetRange().BackColor = value; this.Invalidate(); }
        //}

        //[Category("SelectedSheetRange")]
        //public Color _ForeColor
        //{
        //    get { return GetSelSheetRange().ForeColor; }
        //    set { GetSelSheetRange().ForeColor = value; this.Invalidate(); }
        //}

        //[Category("SelectedSheetRange")]
        //public string _Text
        //{
        //    get
        //    {
        //        object x = GetSelSheetRange().Text;
        //        return (x == null) ? "" : x.ToString();
        //    }
        //    set { GetSelSheetRange().Text = value; this.Invalidate(); }
        //}

        //[Category("SelectedSheetRange")]
        //public int Rows_Height
        //{
        //    get { return mSheet.Rows[mSelectedSheetRange_Row1].Height; }
        //    set
        //    {
        //        for (int i = mSelectedSheetRange_Row1; i <= mSelectedSheetRange_Row2; i++)
        //            mSheet.Rows[i].Height = value;
        //        this.Invalidate();
        //    }
        //}

        //[Category("SelectedSheetRange")]
        //public int Columns_Width
        //{
        //    get { return mSheet.Columns[mSelectedSheetRange_Col1].Width; }
        //    set
        //    {
        //        for (int i = mSelectedSheetRange_Col1; i <= mSelectedSheetRange_Col2; i++)
        //            mSheet.Columns[i].Width = value;
        //        this.Invalidate();
        //    }
        //}

        //[Category("SelectedSheetRange")]
        //public bool _Merged
        //{
        //    get { return GetSelSheetRange().Merged; }
        //    set { GetSelSheetRange().Merged = value; this.Invalidate(); }
        //}

        //[Category("SelectedSheetRange")]
        //public ContentAlignment _TextAlign
        //{
        //    get { return GetSelSheetRange().TextAlign; }
        //    set { GetSelSheetRange().TextAlign = value; this.Invalidate(); }
        //}

        private void SheetBox_Resize(object sender, EventArgs e)
        {
            RefreshImage();
            //this.Invalidate();
        }

        //public int PrintAreaRow
        //{
        //    get { return mSheet.PrintAreaRow; }
        //    set { mSheet.PrintAreaRow = value; this.Invalidate(); }
        //}

        //public int PrintAreaColumn
        //{
        //    get { return mSheet.PrintAreaColumn; }
        //    set { mSheet.PrintAreaColumn = value; this.Invalidate(); }
        //}

        //[Category("SelectedSheetRange")]
        //public int Border_Left
        //{
        //    get { return GetSelSheetRange().GetBorder(SheetRangeBorderTypes.Left); }
        //    set { GetSelSheetRange().SetBorder(SheetRangeBorderTypes.Left, value); this.Invalidate(); }
        //}

        //[Category("SelectedSheetRange")]
        //public int Border_Right
        //{
        //    get { return GetSelSheetRange().GetBorder(SheetRangeBorderTypes.Right); }
        //    set { GetSelSheetRange().SetBorder(SheetRangeBorderTypes.Right, value); this.Invalidate(); }
        //}

        //[Category("SelectedSheetRange")]
        //public int Border_Top
        //{
        //    get { return GetSelSheetRange().GetBorder(SheetRangeBorderTypes.Top); }
        //    set { GetSelSheetRange().SetBorder(SheetRangeBorderTypes.Top, value); this.Invalidate(); }
        //}

        //[Category("SelectedSheetRange")]
        //public int Border_Bottom
        //{
        //    get { return GetSelSheetRange().GetBorder(SheetRangeBorderTypes.Bottom); }
        //    set { GetSelSheetRange().SetBorder(SheetRangeBorderTypes.Bottom, value); this.Invalidate(); }
        //}

        //[Category("SelectedSheetRange")]
        //public int Border_Inner
        //{
        //    get { return GetSelSheetRange().GetBorder(SheetRangeBorderTypes.Inner); }
        //    set { GetSelSheetRange().SetBorder(SheetRangeBorderTypes.Inner, value); this.Invalidate(); }
        //}

        //[Category("SelectedSheetRange")]
        //public int Border_Outer
        //{
        //    get { return GetSelSheetRange().GetBorder(SheetRangeBorderTypes.Outer); }
        //    set { GetSelSheetRange().SetBorder(SheetRangeBorderTypes.Outer, value); this.Invalidate(); }
        //}

        //[Category("SelectedSheetRange")]
        //public int Border_InnerOuter
        //{
        //    get { return GetSelSheetRange().GetBorder(SheetRangeBorderTypes.InnerOuter); }
        //    set { GetSelSheetRange().SetBorder(SheetRangeBorderTypes.InnerOuter, value); this.Invalidate(); }
        //}

        //[Category("SelectedSheetRange")]
        //public int Border_MiddleHorizontal
        //{
        //    get { return GetSelSheetRange().GetBorder(SheetRangeBorderTypes.MiddleHorizontal); }
        //    set { GetSelSheetRange().SetBorder(SheetRangeBorderTypes.MiddleHorizontal, value); this.Invalidate(); }
        //}

        //[Category("SelectedSheetRange")]
        //public int Border_MiddleVertical
        //{
        //    get { return GetSelSheetRange().GetBorder(SheetRangeBorderTypes.MiddleVertical); }
        //    set { GetSelSheetRange().SetBorder(SheetRangeBorderTypes.MiddleVertical, value); this.Invalidate(); }
        //}

        #region IPrintOn Members

        public void PrintOn(Graphics g, Rectangle bounds)
        {
            mSheet.PrintOn(g, bounds, true, true, false);
        }

        #endregion
    }
}
